import axios from 'axios'
import router from '../router'
import global_ from './base.js'
export default async() => {
  const refreshToken = window.sessionStorage.getItem('token_refresh')
  const baseURL = global_.BASE_URL
  try {
    // console.log('beging refreshJwt')
    const res = await axios.post(baseURL + 'index/RefreshJwt/', { refreshJwt: refreshToken })
    // console.log(res)
    const code = Number(res.data.code)
    // console.log(code)
    if (code !== 200) {
      return router.replace({ path: '/login', query: { redirect: router.currentRoute.fullPath }})
    } else {
      // const data = res.data
      // console.log(data)
      const token = res.data.data.token
      // console.log('newtoken=', token)
      const tokenrefresh = res.data.data.token_refresh
      if (token && tokenrefresh) {
        // 1.将登录成功后的token ，保存到客户端的sessionstorage中
        window.sessionStorage.setItem('token', token)
        // 1.将登录成功后的token_refresh ，保存到客户端的sessionstorage中
        window.sessionStorage.setItem('token_refresh', tokenrefresh)
      }
    }
    // console.log('refreshJwt end')
    // return (res)
  } catch (error) {
    console.log('refreshJwt error')
    console.log(error)
  }
}
